﻿using AntdUI;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Dictionary.EventBus;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Column = AntdUI.Column;

namespace Dictionary
{
    public partial class ReferenceForm : AntdUI.Window
    {
        IniClass Ini = new IniClass();
        List<TableColumn> TableList = new List<TableColumn>();
        List<string> ProcessList = new List<string>();
        public ReferenceForm()
        {
            InitializeComponent();
            InitDataLoad();
        }

        public void InitDataLoad()
        {
            // 创建 TabPage 并添加 Table
            var tabPage = new AntdUI.TabPage() { Name = "GI__Technology", Text = "GI__Technology" };
            AddTableToTabPage(tabPage);
            var tabPage1 = new AntdUI.TabPage() { Name = "GI__Process", Text = "GI__Process" };
            // 创建搜索面板
            var searchPanel = new AntdUI.Panel
            {
                Dock = DockStyle.Top,
                Height = 30
            };

            var searchInput = new AntdUI.Input
            {
                Name = "searchInput",
                PlaceholderText = "请输入搜索关键词",
                Dock = DockStyle.Left,
                Width = 200
            };
            searchPanel.Controls.Add(searchInput);

            var searchButton = new AntdUI.Button
            {
                Text = "搜索",
                Dock = DockStyle.Right,
                Type = AntdUI.TTypeMini.Primary,
                Width = 60,
                BackColor = Color.Blue,

                
            };
            // 创建重置按钮
            var resetButton = new AntdUI.Button
            {
                Text = "重置",
                Type = AntdUI.TTypeMini.Primary,
                Dock = DockStyle.Right,
                Width = 80,
                Margin = new Padding(10, 0, 0, 0)
            };
            searchPanel.Controls.Add(searchButton);
            searchPanel.Controls.Add(resetButton);
            tabPage1.Controls.Add(searchPanel);

            AddTableToTabPage(tabPage1);
            var tabPage2 = new AntdUI.TabPage() { Name = "TestName1", Text = "TestName1" };
            AddTableToTabPage(tabPage2);
            var tabPage3 = new AntdUI.TabPage() { Name = "TestName2", Text = "TestName2" };
            AddTableToTabPage(tabPage3);
            var tabPage4 = new AntdUI.TabPage() { Name = "TestName3", Text = "TestName3" };
            AddTableToTabPage(tabPage4);
            var tabPage5 = new AntdUI.TabPage() { Name = "TestName4", Text = "TestName4" };
            AddTableToTabPage(tabPage5);

            tabs1.Pages.Add(tabPage);
            tabs1.Pages.Add(tabPage1);
            tabs1.Pages.Add(tabPage2);
            tabs1.Pages.Add(tabPage3);
            tabs1.Pages.Add(tabPage4);
            tabs1.Pages.Add(tabPage5);


            searchButton.Click += (sender, e) => SearchButton_Click(tabPage1, searchInput.Text);
            resetButton.Click += (sender, e) => ResetButton_Click(tabPage1, searchInput.Text);



            // 订阅事件
            EventBus.OnPageEventTriggered += EventBus_OnPageEventTriggered;
        }

        private void EventBus_OnPageEventTriggered(object sender, EventArgs e)
        {
            // 若传递了自定义参数，可强转解析：
            if (e is DataTran _DataTran)
            {

                foreach (Control controls in tabs1.Controls)
                {
                    if (controls is  AntdUI.TabPage tabPage) {

                        if (tabPage.Name== "GI__Process") {
                            ProcessList = _DataTran.DataTranStringList;
                            List<TableColumn> TableListSearch = new List<TableColumn>();
                            var ProcessListCopy = ProcessList;
                            for (int i = 0; i < ProcessListCopy.Count; i++)
                            {
                                TableColumn Tc = new TableColumn();
                                Tc.Name = ProcessListCopy[i];
                                Tc.CellButton = new CellButton[]
                                {
                                    new CellButton(Guid.NewGuid().ToString(), "复制", TTypeMini.Primary)
                                };
                                TableListSearch.Add(Tc);
                            }


                            foreach (Control ct in tabPage.Controls )
                            {

                                if (ct is AntdUI.Table table)
                                {
                                    table.DataSource = TableListSearch;
                                    table.Invalidate();
                                }
                            }

                        }

                    }
                 
                }
            }
        }

        private void SearchButton_Click(AntdUI.TabPage tab, string text)
        {

            List<TableColumn> TableListSearch = new List<TableColumn>();
            var ProcessListCopy = ProcessList.Where(t => t.Contains(text)).ToList();
            for (int i = 0; i < ProcessListCopy.Count; i++)
            {
                TableColumn Tc = new TableColumn();
                Tc.Name = ProcessListCopy[i];
                Tc.CellButton = new CellButton[]
                {
                     new CellButton(Guid.NewGuid().ToString(), "复制", TTypeMini.Primary)
                };
                TableListSearch.Add(Tc);
            }
            foreach (Control controls in tab.Controls) {

                if (controls is AntdUI.Table table) {
                    table.DataSource = TableListSearch;
                    table.Invalidate();
                }
            }
        }

        private void ResetButton_Click(AntdUI.TabPage tab, string text)
        {

            List<TableColumn> TableListSearch = new List<TableColumn>();

            var ProcessListCopy = ProcessList;
            for (int i = 0; i < ProcessListCopy.Count; i++)
            {
                TableColumn Tc = new TableColumn();
                Tc.Name = ProcessListCopy[i];
                Tc.CellButton = new CellButton[]
                {
                     new CellButton(Guid.NewGuid().ToString(), "复制", TTypeMini.Primary)
                };
                TableListSearch.Add(Tc);
            }

            foreach (Control controls in tab.Controls)
            {

                if (controls is AntdUI.Table table)
                {
                    table.DataSource = TableListSearch;
                    table.Invalidate();

                }

            }
        }

        private void AddTableToTabPage(AntdUI.TabPage tabPage)
        {
            var table= new AntdUI.Table();
            if (tabPage.Name == "GI__Process")
            {
                // 创建 Table 控件
                table = new AntdUI.Table
                {
                    Dock = DockStyle.Fill,
                    Padding = new Padding(0, 35, 0, 0),// 顶部留出搜索面板的高度
                };

            }
            else
            {
                table = new AntdUI.Table
                {
                    Dock = DockStyle.Fill,
                    Location = new Point(0, 0),
                    Size = new Size(tabPage.Width, tabPage.Height)
                };
            }
                table.Columns = new ColumnCollection() {
                new Column("Name", tabPage.Name+"名称") { },
                new Column("CellButton", "操作") { }
            };

            DataBuildFunc(tabPage.Name, table);
            // 添加到 TabPage
            tabPage.Controls.Add(table);

            table.CellButtonClick += Table_base_CellButtonClick;
        }




        //表格内部按钮事件
        private void Table_base_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var buttontext = e.Btn.Text;

            if (e.Record is TableColumn tc)
            {
                var window = this;
                switch (buttontext)
                {
                    case "复制":
                        Clipboard.SetText(tc.Name);
                        AntdUI.Message.info(window, tc.Name + "已复制", autoClose: 1);
                        break;
                }
            }
        }



        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="name"></param>
        /// <param name="table"></param>
        public void DataBuildFunc(string name, AntdUI.Table table)
        {
            TableList.Clear();
      
            int ListCount = Convert.ToInt32(Ini.IniReadValue(name, name + "Count"));
            for (int i = 0; i < ListCount; i++)
            {
                TableColumn Tc = new TableColumn();
                Tc.Name = Ini.IniReadValue(name, name + (i + 1));
                Tc.CellButton = new CellButton[]
                {
                     new CellButton(Guid.NewGuid().ToString(), "复制", TTypeMini.Primary)
                };
                TableList.Add(Tc);


                if (name == "GI__Process")
                {
                    ProcessList.Add(Tc.Name);
                }
            }
            table.DataSource = TableList;
        }


        public class TableColumn : NotifyProperty
        {

            public string Name { get; set; }
            /// <summary>
            /// 按钮
            /// </summary>
            private CellLink[] cellButton;
            public CellLink[] CellButton
            {
                get { return cellButton; }
                set
                {
                    if (cellButton == value) return;
                    cellButton = value;
                    OnPropertyChanged(nameof(CellButton));
                }
            }
        }

        /// <summary>
        /// 机型上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bn_upload_Click_1(object sender, EventArgs e)
        {
            UploadModel uploadModel = new UploadModel();
            uploadModel.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process.Start("https://smartwhp.lenovo.com/ioc/#/operation/standard/model");
        }

        private void bn_test_Click_1(object sender, EventArgs e)
        {
            DictionaryConfig config = new DictionaryConfig();
            config.Show();
        }
    }
}
